declare global {
  interface Window {
    previousMatrix?: number[][];
  }
}

interface DailyReport {
  do_programe_rate: string;
  do_read_rate: string;
  do_sport_rate: string;
  eat_on_time_rate: string;
  index_type: string;
  make_plan_rate: string;
  not_smoke_rate: string;
  not_wank_rate: string;
  replay_date: string;
}

// 获取昨天的日期
function getYesterdayDate(): string {
  const date = new Date();
  date.setDate(date.getDate() - 1);
  return date.toISOString().split('T')[0];
}

// 从API获取数据
async function fetchDailyReport(): Promise<DailyReport[]> {
  const yesterday = getYesterdayDate();
  const response = await fetch(`http://timelooper.icu:5011/api/daily_report?replay_date=${yesterday}`);
  if (!response.ok) {
    throw new Error('Failed to fetch daily report');
  }
  return response.json();
}

// Generate data for the dashboard
export async function generateMockData() {
  try {
    const reports = await fetchDailyReport();
    const matrix = reports.map(report => [
      parseFloat(report.make_plan_rate),
      parseFloat(report.do_read_rate),
      parseFloat(report.do_sport_rate),
      parseFloat(report.do_programe_rate),
      parseFloat(report.not_smoke_rate),
      parseFloat(report.eat_on_time_rate),
      parseFloat(report.not_wank_rate)
    ]);
    const leftDate = reports.map(report => report.index_type);

    const achievements = [
      {
        id: "achievement1",
        name: "First Milestone",
        unlocked: Math.random() > 0.5,
        progress: Math.random() * 100,
      },
      {
        id: "achievement2",
        name: "Data Master",
        unlocked: Math.random() > 0.7,
        progress: Math.random() * 100,
      },
      {
        id: "achievement3",
        name: "Perfect Score",
        unlocked: Math.random() > 0.8,
        progress: Math.random() * 100,
      },
      {
        id: "achievement4",
        name: "Consistent Growth",
        unlocked: Math.random() > 0.6,
        progress: Math.random() * 100,
      },
      {
        id: "achievement5",
        name: "Data Explorer",
        unlocked: Math.random() > 0.5,
        progress: Math.random() * 100,
      },
      {
        id: "achievement6",
        name: "Analytics Pro",
        unlocked: Math.random() > 0.7,
        progress: Math.random() * 100,
      },
      {
        id: "achievement7",
        name: "Trend Spotter",
        unlocked: Math.random() > 0.8,
        progress: Math.random() * 100,
      },
      {
        id: "achievement8",
        name: "Full Spectrum",
        unlocked: Math.random() > 0.9,
        progress: Math.random() * 100,
      },
    ]

    return {
      matrix,
      previousMatrix: typeof window !== 'undefined' ? (window.previousMatrix || matrix) : matrix,
      achievements,
      leftDate
    }
  } catch (error) {
    console.error('Error fetching data:', error);
    // 如果API调用失败，返回默认数据
    return {
      matrix: Array(4).fill(0).map(() => Array(7).fill(0)),
      previousMatrix: Array(4).fill(0).map(() => Array(7).fill(0)),
      achievements: [],
      leftDate: [] 
    }
  }
}

// Store the current matrix for comparison on next update
export function storeCurrentMatrix(matrix: number[][]) {
  if (typeof window !== 'undefined') {
    window.previousMatrix = matrix;
  }
}

